package org.forever.learn.functioncall;



import jakarta.annotation.Resource;
import org.forever.learn.util.DayCountFunction;
import org.forever.learn.util.DayWeatherFunction;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.model.function.FunctionCallbackWrapper;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.ollama.api.OllamaOptions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/ai/function")
public class FunctionCallController {
//    @Resource
//    ChatClient chatClient;

    @Resource
    OllamaChatModel ollamaChatModel;


    @GetMapping("/getDayBalance")
    public String chatWithFunction(@RequestParam String prompt, @RequestParam String sessionId) {
        OllamaOptions ollamaOptions = OllamaOptions.builder()
                .withFunctionCallbacks(List.of(
                        FunctionCallbackWrapper.builder(new DayWeatherFunction())
                                .withName("dayWeatherFunction") // (1) function name
                                .withDescription("获取指定日期的天气") // (2) function description
                                .build(),
                        FunctionCallbackWrapper.builder(new DayCountFunction())
                                .withName("dayCountFunction") // (1) function name
                                .withDescription("获取指定邮箱的收入和支出") // (2) function description
                                .build()));

        ChatResponse chatResponse = ollamaChatModel.call(new Prompt(prompt, ollamaOptions));
        return chatResponse.getResult().getOutput().getContent();
//        return chatResponse.toString();
//        return ChatClient.create(ollamaChatModel).prompt().user(prompt).functions("dayWeatherFunction","dayCountFunction").call().content();
//        return chatClient.prompt()
//                .user(prompt)
//                .functions()
//                .call()
//                .content();

    }

}
